package com.perfectbeauties.admin.parser;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

public class ParserUtil {

private static final String SUBMIT_URL = "http://perfect-beauties.com/admin/new.php";
	
	public static String submit(String name, String desc, int siteId, String zip) throws Exception {
		Map<String, String> m = new HashMap<String, String>();
		m.put("name", name);
		m.put("desc", desc);
		m.put("site_id", ""+siteId);
		m.put("zip", zip);
		return submit(m);
	}
	
	public static String submit(Map<String, String> params) throws Exception {
		
		 String content = new String("auth=codePBe1&script=true");
		 for (String key : params.keySet()) {
			 content += "&" + key + "=" + params.get(key);
		 }
		
		URL                 url;
	    URLConnection   urlConn;
	    DataOutputStream    printout;
	    DataInputStream     input;
	    // URL of CGI-Bin script.
	    url = new URL (SUBMIT_URL);
	    // URL connection channel.
	    urlConn = url.openConnection();
	    // Let the run-time system (RTS) know that we want input.
	    urlConn.setDoInput (true);
	    // Let the RTS know that we want to do output.
	    urlConn.setDoOutput (true);
	    // No caching, we want the real thing.
	    urlConn.setUseCaches (false);
	    // Specify the content type.
	    urlConn.setRequestProperty
	    ("Content-Type", "application/x-www-form-urlencoded");
	    // Send POST output.
	    printout = new DataOutputStream (urlConn.getOutputStream ());
	   
	    printout.writeBytes (content);
	    printout.flush ();
	    printout.close ();
	    
	    input = new DataInputStream (urlConn.getInputStream ());
	    String str = input.readLine();
	    input.close ();
	    return str;
	}
}
